/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.dal;

import entity.Role;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Hailk_c00639
 */
public class RoleDAL {

    MyConnection con = new MyConnection();

    public List<Role> getAllRole() {
        List<Role> lst = new ArrayList<>();

        try {
            CallableStatement call = con.getConnection().prepareCall("{call sp_GetAllRole}");
            ResultSet rs = call.executeQuery();
            while (rs.next()) {
                Role role = new Role();
                role.setRoleID(rs.getInt("RoleID"));
                role.setRoleName(rs.getString("RoleName"));

                lst.add(role);
            }

        } catch (SQLException ex) {
            Logger.getLogger(RoleDAL.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally
        {
            con.closeDB();
        }
        return lst;

    }

    public Role getRoleByID(int roleID) {
        Role role = null;

        try {
            CallableStatement call = con.getConnection().prepareCall("{call sp_GetRoleByID(?)}");
            call.setInt(1, roleID);
            ResultSet rs = call.executeQuery();
            if (rs.next()) {
                role = new Role();
                role.setRoleID(rs.getInt("RoleID"));
                role.setRoleName(rs.getString("RoleName"));
            } else {
                // Neu sai thi thong bao loi
            }

        } catch (SQLException ex) {
            Logger.getLogger(RoleDAL.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally
        {
            con.closeDB();
        }
        return role;

    }
}
